package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.ActorDao;
import cn.edu.sdjzu.xg.bysj.dao.TeacherDao;
import cn.edu.sdjzu.xg.bysj.dao.UserDao;
import cn.edu.sdjzu.xg.bysj.domain.Teacher;
import cn.edu.sdjzu.xg.bysj.domain.User;
import cn.edu.sdjzu.xg.bysj.domain.authority.Actor;
import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public final class TeacherService {
	//获取TeacherDao实例
	private static TeacherDao teacherDao= TeacherDao.getInstance();
	//新建TeacherService对象
	private static TeacherService teacherService=new TeacherService();
	//构造器
	private TeacherService(){}
	//获得TeacherService对象
	public static TeacherService getInstance(){
		return teacherService;
	}

	/**
	 * 向teacherDao发消息根据条件和分页查找
	 * @param conditions
	 * @param pagination
	 * @return
	 * @throws SQLException
	 */
	public Collection<Teacher> findAll(Collection<Condition> conditions, Pagination pagination) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		Collection<Teacher> teachers = null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			teachers = teacherDao.findAll(connection,conditions,pagination);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return teachers;
	}

	/**
	 * 向teacherDao发消息根据id查找
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Teacher find(Integer id) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		Teacher teacher =null;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			teacher = teacherDao.find(connection,id);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return teacher;
	}

	/**
	 * 向teacherDao发消息更新
	 * @param teacher
	 * @return
	 * @throws SQLException
	 */
	public boolean update(Teacher teacher) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		boolean updated = false;
		//关闭自动提交，事务开始
		connection.setAutoCommit(false);
		try {
			updated = teacherDao.update(teacher,connection);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return updated;
	}

	/**
	 * 添加Teacher对象
	 * @param teacher 要添加的teacher对象
	 * @return 添加成功给返回true，失败：false
	 * @throws SQLException
	 */
	public boolean add(Teacher teacher) throws SQLException, BysjException {
		//获得进行事务的链接，添加教师和用户都是用本连接对象
		Connection connection = JdbcHelper.getConn();
		//关闭自动提交，开始事务
		connection.setAutoCommit(false);
		boolean added = false;
		try {
			int actor_id = ActorDao.getInstance().add(connection);
			teacher.setId(actor_id);
			//添加教师对象，返回值为自动生成的id值
			TeacherDao.getInstance().add(teacher,connection);
			//创建用户对象，用户名与密码取自teacher的字段值no
			User user = new User(teacher.getNo(),teacher.getNo(),null,teacher);
			//增加用户
			added = UserDao.getInstance().add(connection,user);
			ActorDao.getInstance().update(connection,teacher);
			//提交事务
			connection.commit();
		}catch (SQLException e){
			//回滚所有的操作
			connection.rollback();
			throw new BysjException("新添加的教师的编号已经存在");
		}finally {
			//开启自动提交，结束事务
			connection.setAutoCommit(true);
			//关闭数据库连接
			JdbcHelper.close(null,connection);
		}
		return added;

	}

	/**
	 * 向teacherDao发消息删除
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public boolean delete(Integer id) throws SQLException {
		//获取数据库连接
		Connection connection = JdbcHelper.getConn();
		boolean deleted = false;
		//关闭自动提交，开始事务
		connection.setAutoCommit(false);
		try {
			deleted = teacherDao.delete(id,connection);
			//提交事务
			connection.commit();
		}
		catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
		}
		finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			//释放连接
			JdbcHelper.close(connection);
		}
		return deleted;
	}
}
